package com.dbq.fast.view;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dbq.fast.R;

/**
 * 带提交按钮的edittext.edit封装下业务逻辑 只限定15个字符控制等
 * Created by Administrator on 2018/1/3 0003.
 */

public class EditTextWithSubmit extends RelativeLayout implements View.OnClickListener {
    EditText input;
    TextView submit;
    private final Context mContext;
    private EditTextWithSubmitAction mEditTextWithSubmitAction;

    public EditTextWithSubmit(Context context) {
        super(context);
        mContext=context;
        init();
    }

    private void init() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.customer_view_edittextwithsubmit, this,true);
        submit = (TextView) view.findViewById(R.id.submit);
        input = (EditText) view.findViewById(R.id.input);
        initListener();
    }

    private void initListener() {
        submit.setOnClickListener(this);
        input.addTextChangedListener(textWatcher);
    }

    public EditTextWithSubmit(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext=context;
        init();
    }

    public EditTextWithSubmit(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext=context;
        init();
    }



    @Override
    public void onClick(View view){
        switch (view.getId()){
            case R.id.submit:
                if(mEditTextWithSubmitAction!=null){
                    mEditTextWithSubmitAction.submit(input.getText().toString());
                }
                break;
        }
    }

    private TextWatcher textWatcher = new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };


    public interface EditTextWithSubmitAction{
        void submit(String shareCode);
    }

    public void setEditTextWithSubmitAction(EditTextWithSubmitAction editTextWithSubmitAction) {
        mEditTextWithSubmitAction = editTextWithSubmitAction;
    }
}
